#!/usr/bin/perl
# Leandro Linardos | 89567

our $CURRDIR = "%%GRUPO%%";
our $BENEFICIARIES_DIR = "${CURRDIR}/nuevos/";
our $OUTPUT_DIR = "${CURRDIR}/list/";   
our $AGENCIES_MASTER_FILE = "${CURRDIR}/data/agencias.mae"; 
our $BENEFITS_MASTER_FILE = "${CURRDIR}/data/beneficios.mae";

our $BINDIR = `grep "BINDIR = " "${CURRDIR}/conf/Instula.conf"`;
chomp $BINDIR;
$BINDIR = substr($BINDIR, length("BINDIR = "), length($BINDIR) - length("BINDIR = "));

##
## Los beneficiarios a listar se van agregando a un array mientras se va filtrando.
## Finalmente se procede a mostrar.
##

###############################################################################
###############################################################################

require "${BINDIR}/plist_utils/out_control.pl";
require "${BINDIR}/plist_utils/in_and_validations.pl";
require "${BINDIR}/plist_utils/error_control.pl";
require "${BINDIR}/plist_utils/datamanage.pl";

# Variables para manejo de errores.
our $ERROR_STATUS = 0;
our $ERROR_MSG = "";

# Variables de control, opciones y parámetros. Globales para acceso general.
our $COMMAND_INVOQUED = "$0 @ARGV";
our $T_FLAG = 0;
our $D_FLAG = 0;
our $E_FLAG = "";
our $C_FLAG = 0;
our $H_FLAG = 0;
our $FILES_OPTION = "";
our $BENEFITS_OPTION = "";
our $AGENCIES_OPTION = "";

our @IN_BENEF_FILES = ();
our %AGENCIES_FILTERS = ();
our %BENEFITS_FILTERS = ();
our %STATE_FILTERS = ();

###############################################################################
##                       MAIN                                                ##
###############################################################################

# variable de almacenamiento de elementos efectivos de informe
@filtered_beneficiaries = ();
# variables para la matriz de control
%benefits;
%provinces;
%matrix_control;

do parse_options();
do show_error_info_and_exit() if ( $ERROR_STATUS != 0 );

if($H_FLAG) {
    show_help_msg();
    show_error_info_and_exit();
}

do validate_beneficiaries_files();
do show_error_info_and_exit() if ( $ERROR_STATUS != 0 );

do validate_agencies_filters();
do show_error_info_and_exit() if ( $ERROR_STATUS != 0 );

do validate_benefits_filters();
do show_error_info_and_exit() if ( $ERROR_STATUS != 0 );

# Cargo el array procesando los archivos de entrada.
for( my $i=0; $i <= $#IN_BENEF_FILES; $i++) {
    my $actual_path = "${BENEFICIARIES_DIR}$IN_BENEF_FILES[$i]";
    open(BENEFICIARIES_FILE, "<$actual_path") or die "ERROR AL ABRIR $actual_path\n";
    while(my $line = <BENEFICIARIES_FILE>) {
        my @actual_item = do get_formatted_array_to_report($line);
        if( pass_filters(\@actual_item) ) {
            push(@filtered_beneficiaries, \@actual_item);   
            if( $C_FLAG ) {
                my $province = $actual_item[4];
                my $benefit = $actual_item[0];
                refresh_control_matrix($province, $benefit, \%matrix_control,
                                       \%provinces, \%benefits);
            }
        } 
    }
}

my $control_matrix = get_control_matrix(\%matrix_control, \%provinces, \%benefits) if $C_FLAG;

if ($T_FLAG) {
    do show_results(*filtered_beneficiaries);
    print $control_matrix if $C_FLAG;
}
if ($D_FLAG) {
    my $sequence = get_new_sequence_number();
    my $path = "${OUTPUT_DIR}plist.${sequence}";   
    do report_output_file(*filtered_beneficiaries, $path);
    if ($C_FLAG) {
        open FILE, ">>${path}" or die "ERROR AL ABRIR $path";
        print FILE $control_matrix;
        close FILE;
    }
}

show_error_info_and_exit();
